package com.feiyun.service.impl;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.feiyun.common.PageParam;
import com.feiyun.mapper.TempUserMapper;
import com.feiyun.mapper.UploadUserInfoMapper;
import com.feiyun.mapper.UserFileRelationMapper;
import com.feiyun.mapper.UserMapper;
import com.feiyun.model.entity.TempUser;
import com.feiyun.model.entity.User;
import com.feiyun.model.entity.UserFileRelation;
import com.feiyun.service.ITempUserService;
import com.feiyun.util.LocalDateUtils;
import com.feiyun.util.JWTUtils;
import com.feiyun.util.UUIDUtils;

/**
 * @ClassName UserServiceImpl
 * @author: xingming
 * @Description:
 * @time 2023/03/12 17:27:21
 * @Version 1.0
 */
@Service
@Slf4j
@RequiredArgsConstructor
public class TempUserServiceImpl implements ITempUserService {

    private final TempUserMapper tempUserMapper;

    private final UserMapper userMapper;

    private final JWTUtils jwtUtils;

    @Resource
    private UploadUserInfoMapper uploadUserInfoMapper;

    @Resource
    private UserFileRelationMapper userFileRelationMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveTempUser(TempUser tempUser) {
        tempUser.setUuid(UUIDUtils.generate32UUID());
        tempUserMapper.saveTempUser(tempUser);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public User login(TempUser tempUser) {
        User user = userMapper.selectByWxId(tempUser);
        if (user != null) {
            // 登录成功存储Token
            user.setToken(jwtUtils.createToken(user.getId()));
            return user;
        } else {
            this.saveTempUser(tempUser);
            return null;
        }
    }

    /**
     * 条件查询
     *
     * @param tempUser 条件查询实体
     * @return 符合条件的用户
     */
    @Override
    public List<TempUser> selectUserByCondition(TempUser tempUser) {
        QueryWrapper<TempUser> wrapper = new QueryWrapper<>();
        return tempUserMapper.selectList(wrapper);
    }

    /**
     * 分页查询
     *
     * @param pageParam
     */
    @Override
    public List<User> selectUploadUserInfo(PageParam<UserFileRelation> pageParam) {
        // 根据file id 查到user id
        QueryWrapper<UserFileRelation> userFileRelationQueryWrapper = new QueryWrapper<>();
        userFileRelationQueryWrapper.eq("file_id", pageParam.getParam().getFileId());
        List<UserFileRelation> userFileRelations = userFileRelationMapper.selectList(userFileRelationQueryWrapper);

        List<Integer> userIdList = new ArrayList<>(userFileRelations.size());
        userFileRelations.forEach(userFileRelation -> userIdList.add(userFileRelation.getUserId()));

        List<User> userList = userMapper.selectBatchIds(userIdList);
        return userList;

    }

    /**
     * 修改tempuser 信息
     *
     * @param user
     */
    @Override
    public void updateTempUserInfo(User user) {
        user.setUpdateTime(LocalDateUtils.getNowDate());
        userMapper.updateById(user);
    }

    @Override
    public String findFileIdByUploadUser(String userId) {
        QueryWrapper<UserFileRelation> userFileRelationQueryWrapper = new QueryWrapper<>();
        userFileRelationQueryWrapper.eq("user_id", userId);
        List<UserFileRelation> userFileRelations = userFileRelationMapper.selectList(userFileRelationQueryWrapper);
        return userFileRelations.get(0).getFileId();
    }
}